#!/bin/bash
# Author: Xun Deng

os_name='initrd'
rootfs=ubuntu-focal-oci-amd64-root.tar.gz
rm -rf $os_name

[ -e $rootfs ] || wget https://partner-images.canonical.com/oci/focal/current/ubuntu-focal-oci-amd64-root.tar.gz
if [ -d $os_name.origin ]
then
    cp -rp $os_name.origin $os_name
else
    mkdir -p $os_name
    tar xzf $rootfs -C $os_name

    mount --bind /dev $os_name/dev
    mount --bind /run $os_name/run
    cp chroot_config.sh $os_name
    chroot $os_name bash chroot_config.sh
    rm $os_name/chroot_config.sh
    umount $os_name/dev
    umount $os_name/run

    cp -rp $os_name $os_name.origin
fi

find $os_name -name '*apt*' | tac | xargs rm -rf
find $os_name -name '*libicu*' | tac | xargs rm -rf
find $os_name -name '*gconv*' | tac | xargs rm -rf
find $os_name -name '*dpkg*' | tac | xargs rm -rf
find $os_name -name '*debconf*' | tac | xargs rm -rf
find $os_name -name '*X11*' | tac | xargs rm -rf
find $os_name -name '*sftp*' | tac | xargs rm -rf
find $os_name -name '*pydoc*' | tac | xargs rm -rf
find $os_name -name '*pdb*' | tac | xargs rm -rf
# find $os_name -name '*freedesktop*' | tac | xargs rm -rf

# find $os_name -name '*perl*' | tac | xargs rm -rf
# find $os_name -name '*.pl' | tac | xargs rm -rf
# find $os_name -name '*dash*' | tac | xargs rm -rf
# find $os_name -name 'file' | tac | xargs rm -rf
# rm $os_name/usr/bin/{systemd-analyze,openssl,wget,gpgv,ssh-keyscan,ssh-add,ssh-agent,localedef,diff,install,man}
# rm $os_name/usr/bin/{mawk,vdir,dir,top,sort,lsblk,partx,gzip,date,factor,sha*,tic}
# rm -rf $os_name/lib/python3.8/{test,unittest,email,http,multiprocessing,html,doctest.py,urllib}
# find $os_name -name '*terminfo*' | tac | xargs rm -rf # cannot run 'clear' if deleted

rm $os_name/usr/bin/systemd-analyze

find $os_name -name '*python*' | tac | xargs rm -rf
find $os_name -name '*py*3*' | tac | xargs rm -rf
find $os_name -name '*.py' | tac | xargs rm -rf
find $os_name -name '*networkd-dispatcher*' | tac | xargs rm -rf
rm -rf $os_name/usr/share/pyshared
rm -rf $os_name/usr/share/doc
